<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

/**
 * Main helper aplikácie.
 * 
 * @author Jakub Čulík
 * @author Michal Foltin
 */

/**
 * Funkcia overi ci je pouzivatel prihlaseny
 * @return boolean - true ak je prihlaseny inak false
 */
function isLogged() {
  $ci = & get_instance();
  if (isset($ci->session->userdata) && $ci->session->userdata['logged_in'])
    return true;
  return false;
}

/**
 * Funkcia overi ci je pouzivatel admin
 * @return boolean - true ak je admin inak false
 */
function isAdmin() {
  $ci = & get_instance();
  if (isset($ci->session->userdata) && $ci->session->userdata['logged_in'] && $ci->session->userdata['isAdmin'])
    return true;
  return false;
}

/**
 * Funkcia podla id vrati nazov typu eventu
 * @param type $id - id typu eventu
 * @return type - string nazov typu eventu
 */
function getEventType($id) {
  $ci = & get_instance();
  $ci->load->model('user_model');
  $types = $ci->user_model->getEventTypes();
  return $types[$id - 1]['event_type'];
}

/**
 * Metóda testuje či počas vykonávania dotazu v databáze nastala chyba.
 * 
 * @return boolean - true ak nastala chyba, false ak nenastala chyba
 */
function wasError() {
  $ci = & get_instance();
  if ($ci->db->trans_status() === FALSE) {
    setError();
    $ci->db->trans_rollback();
    return true;
  } else {
    return false;
  }
}

/**
 * Metóda uloží do session informácie o chybe, ktorá sa vyskytla pri vykonávaní dopytu do databázy.
 */
function setError() {
  $ci = & get_instance();
  $db_error = $ci->db->_error_message();
  $db_errno = $ci->db->_error_number();
  $dbError = array(
      'db_error' => $db_error,
      'db_errno' => $db_errno
  );
  $ci->session->set_userdata($dbError);
}

/**
 * Funkcia vrati nazov miestnosti podla id miestnosti
 * @return type - string 
 */
function getRoomName($id) {
  $ci = & get_instance();
  $ci->load->model('user_model');
  $room = $ci->user_model->getRoom($id);
  return $room['room_name'];
}

function smart_dump($parm) {
  echo "<pre>";
  var_dump($parm);
  echo "</pre>";
}

function getDay($param) {
  if (strlen($param) > 1)
    return getNormalDate($param);
  switch ($param) {
    case '1': return 'pondelok';
    case '2': return 'utorok';
    case '3': return 'streda';
    case '4': return 'štvrtok';
    case '5': return 'piatok';
    default : return '-';
  }
}

function ordinal_number_in_week($date) {
  list($day, $month, $year) = explode("-", $date);

  // Get the weekday of the given date
  $wkday = date('l', mktime('0', '0', '0', $month, $day, $year));

  switch ($wkday) {
    case 'Monday':
      return "1";
    case 'Tuesday':
      return "2";
    case 'Wednesday':
      return "3";
    case 'Thursday':
      return "4";
    case 'Friday':
      return "5";
  }
}

function week_from_monday($date) {
  // Assuming $date is in format DD-MM-YYYY
  list($day, $month, $year) = explode("-", $date);

  // Get the weekday of the given date
  $wkday = date('l', mktime('0', '0', '0', $month, $day, $year));

  switch ($wkday) {
    case 'Monday': $numDaysToMon = 0;
      break;
    case 'Tuesday': $numDaysToMon = 1;
      break;
    case 'Wednesday': $numDaysToMon = 2;
      break;
    case 'Thursday': $numDaysToMon = 3;
      break;
    case 'Friday': $numDaysToMon = 4;
      break;
    case 'Saturday': $numDaysToMon = 5;
      break;
    case 'Sunday': $numDaysToMon = 6;
      break;
  }

  // Timestamp of the monday for that week
  $monday = mktime('0', '0', '0', $month, $day - $numDaysToMon, $year);

  $seconds_in_a_day = 86400;

  // Get date for 7 days from Monday (inclusive)
  for ($i = 0; $i < 7; $i++) {
    $dates[$i] = date('d-m-Y', $monday + ($seconds_in_a_day * $i));
  }

  return $dates;
}

function getRegularDate($dates) {
  $result = array();

  for ($i = 0; $i < 7; $i++) {
    $all = explode('-', $dates[$i]);

    $d = $all[0];
    $m = $all[1];

    if (substr($d, 0, 1) == '0')
      $d = substr($d, 1);
    if (substr($m, 0, 1) == '0')
      $m = substr($m, 1);
    $result[$i] = $d . '-' . $m . '-' . $all[2];
  }

  return $result;
}

function changeFormatDate($date) {
  $all = explode('-', $date);

  if (strlen($all[0]) == 1) {
    $all[0] = '0' . $all[0];
  }

  if (strlen($all[1]) == 1) {
    $all[1] = '0' . $all[1];
  }

  $new_date = $all[2] . '-' . $all[1] . '-' . $all[0];
  return $new_date;
}

function addzeroToDate($date) {
  if (strlen($date) == 1) {
    $date = '0' . $date;
  }
  return $date;
}

function getNormalDate($date) {
  $all = explode('-', $date);

  $d = $all[2];
  $m = $all[1];

  if (substr($d, 0, 1) == '0'){
    $d = substr($d, 1);
  }
  if (substr($m, 0, 1) == '0'){
    $m = substr($m, 1);
  }
  return $d . '-' . $m . '-' . $all[0];
}

function getUserName($id) {
  $ci = & get_instance();
  $ci->load->model('admin_model');

  $data = array(
      'user_id' => array($id),
  );
  $user = $ci->admin_model->getUsers($data);

  return $user[0];
}

function getNames($ids){
  $result = array();
  if(!empty($ids)){
    foreach($ids AS $id){
      $user = getUserName($id);
      $result[] = $user['surname'] . " " .$user['first_name'];
    }
   }
  return $result;
}

?>
